package co.yixiang.modules.shop.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import co.yixiang.constant.ShopConstants;
import co.yixiang.enums.CommonEnum;
import co.yixiang.exception.ErrorRequestException;
import co.yixiang.modules.monitor.service.RedisService;
import co.yixiang.modules.order.web.dto.*;
import co.yixiang.modules.order.web.param.InvestOrderParam;
import co.yixiang.modules.shop.entity.*;
import co.yixiang.modules.shop.mapper.*;
import co.yixiang.modules.shop.mapping.InvestOrderMap;
import co.yixiang.modules.shop.service.*;
import co.yixiang.modules.shop.web.param.YxInvestOrderQueryParam;
import co.yixiang.modules.shop.web.param.YxSalesCommissionDetailQueryParam;
import co.yixiang.modules.shop.web.param.YxStoreProductQueryParam;
import co.yixiang.modules.shop.web.vo.YxInvestCartQueryVo;
import co.yixiang.modules.shop.web.vo.YxInvestOrderQueryVo;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.web.vo.Paging;
import co.yixiang.modules.shop.web.vo.YxSalesCommissionDetailQueryVo;
import co.yixiang.modules.shop.web.vo.YxStoreCartQueryVo;
import co.yixiang.utils.OrderUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 认购订单表 服务实现类
 * </p>
 *
 * @author hisen
 * @since 2020-06-10
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class YxInvestOrderServiceImpl extends BaseServiceImpl<YxInvestOrderMapper, YxInvestOrder> implements YxInvestOrderService {

    @Autowired
    private YxInvestOrderMapper yxInvestOrderMapper;
    @Autowired
    private YxStoreProductMapper yxStoreProductMapper;
    @Autowired
    private YxStoreProductRelationMapper yxStoreProductRelationMapper;


    @Autowired
    private RedisService redisService;
    @Autowired
    private InvestOrderMap investOrderMap;
    @Autowired
    private YxInvestCartMapper yxInvestCartMapper;
    @Autowired
    private YxInvestOrderCartInfoService yxInvestOrderCartInfoService;
    @Autowired
    private YxSalesService yxSalesService;
    @Autowired
    private YxInvestCustomerService yxInvestCustomerService;
    @Autowired
    private YxInvestCustomerMapper yxInvestCustomerMapper;
    @Autowired
    private YxSystemConfigServiceImpl yxSystemConfigServiceImpl;
    @Autowired
    private  YxSalesCommissionDetailService yxSalesCommissionDetailService;
    @Autowired
    private  YxStoreProductService yxStoreProductService;
    @Autowired
    private  YxStoreProductRelationService yxStoreProductRelationService;


    @Override
    public YxInvestOrderQueryVo getYxInvestOrderById(Serializable id) throws Exception {
        return yxInvestOrderMapper.getYxInvestOrderById(id);
    }

    @Override
    public Paging<YxInvestOrderQueryVo> getYxInvestOrderPageList(YxInvestOrderQueryParam yxInvestOrderQueryParam) throws Exception {
        Page page = setPageParam(yxInvestOrderQueryParam, OrderItem.desc("create_time"));
        IPage<YxInvestOrderQueryVo> iPage = yxInvestOrderMapper.getYxInvestOrderPageList(page, yxInvestOrderQueryParam);
        return new Paging(iPage);
    }

    /**
     * 缓存订单
     *
     * @param uid      uid
     * @param cartInfo cartInfo
     * @return
     */
    @Override
    public String cacheOrderInfo(int uid, List<YxInvestCartQueryVo> cartInfo, Double price) {
        String key = IdUtil.simpleUUID();
        InvestCacheDTO cacheDTO = new InvestCacheDTO();
        cacheDTO.setCartInfo(cartInfo);
        cacheDTO.setPrice(price);
        redisService.saveCode("invest_order_" + uid + key, cacheDTO, 1000L);
        return key;
    }

    /**
     * 订单信息
     *
     * @param unique
     * @param uid
     * @return
     */
    @Override
    public YxInvestOrderQueryVo getOrderInfo(String unique, int uid) {
        QueryWrapper<YxInvestOrder> wrapper = new QueryWrapper<>();
        wrapper.and(
                i -> i.eq("order_id", unique).or().eq("`unique`", unique));
        if (uid > 0) wrapper.eq("sales_man_id", uid);

        return investOrderMap.toDto(yxInvestOrderMapper.selectOne(wrapper));
    }
    /**
     * 订单信息
     *
     * @param unique
     * @return
     */
    @Override
   public  YxInvestOrder getOrderInfo(String unique){
        QueryWrapper<YxInvestOrder> wrapper = new QueryWrapper<>();
        wrapper.and(
                i -> i.eq("order_id", unique).or().eq("`unique`", unique));
        return  yxInvestOrderMapper.selectOne(wrapper);
    }


    /*
    * 创建订单
    * @param uid uid
    * @param key key
    * @param param param
    * @return
            */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public YxInvestOrder createOrder(int uid, String key, InvestOrderParam param) {
        YxSales userInfo = yxSalesService.getById(uid);
        if (ObjectUtil.isNull(userInfo)) throw new ErrorRequestException("用户不存在");

        InvestCacheDTO cacheDTO = getCacheOrderInfo(uid, key);
        if (ObjectUtil.isNull(cacheDTO)) {
            throw new ErrorRequestException("订单已过期,请刷新当前页面");
        }

        List<YxInvestCartQueryVo> cartInfo = cacheDTO.getCartInfo();
        Double price = cacheDTO.getPrice();
        Integer totalNum = cartInfo.size();
        List<String> cartIds = new ArrayList<>();
        for (YxInvestCartQueryVo cart : cartInfo) {
            cartIds.add(cart.getId().toString());
            //totalNum += cart.getCartNum();
        }

        //生成分布式唯一值
        String orderSn = IdUtil.getSnowflake(0, 0).nextIdStr();
        //组合数据
        YxInvestOrder investOrder = new YxInvestOrder();
        investOrder.setSalesManId(uid);
        investOrder.setOrderId(orderSn);
        investOrder.setTotalNum(totalNum);

        investOrder.setCartId(StrUtil.join(",", cartIds));
      //  investOrder.setTotalNum(totalNum);
        investOrder.setPrice(price);

        investOrder.setPaid(0);
        investOrder.setPayType(param.getPayType());

        investOrder.setAddTime(new Date());
        investOrder.setUnique(key);
        investOrder.setContractUrl(param.getContractUrl());
        investOrder.setInvestBankCard(param.getInvestBankCard());
        investOrder.setInvestBankName(param.getInvestBankName());
        investOrder.setBankPicUrl(param.getBankPicUrl());
        investOrder.setIdcardFrontPicUrl(param.getIdcardFrontPicUrl());
        investOrder.setIdcardReversePicUrl(param.getIdcardReversePicUrl());
        investOrder.setSignPicUrl(param.getSignPicUrl());
        investOrder.setInvestName(param.getInvestName());
        investOrder.setInvestPhone(param.getInvestPhone());
        investOrder.setInvestIdcard(param.getInvestIdcard());
        investOrder.setSalesManId(userInfo.getUid());
        investOrder.setSalesAvatar(userInfo.getAvatar());
        investOrder.setSalesManName(userInfo.getRealName());
        investOrder.setSalesPhone(userInfo.getPhone());
        investOrder.setAdder(userInfo.getRealName());
        investOrder.setPayOrg(param.getPayOrg());
        investOrder.setOrderTradeNo(key);
        int hesitationDate = Integer.parseInt(yxSystemConfigServiceImpl.getData("invest_hesitation_date"));
        int expireMonth = Integer.parseInt(yxSystemConfigServiceImpl.getData("expire_month"));
        Calendar dayTime = Calendar.getInstance();
        dayTime.setTime(new Date());
        dayTime.add(Calendar.DAY_OF_MONTH, hesitationDate);
     //   dayTime.add(Calendar.DAY_OF_MONTH, 7);
        Calendar monthTime = Calendar.getInstance();
        monthTime.setTime(dayTime.getTime());
        monthTime.add(Calendar.MONTH, expireMonth);
        investOrder.setHesitationDate(hesitationDate);
        investOrder.setExpireMonth(expireMonth);
        investOrder.setBeignDate(dayTime.getTime());
        investOrder.setEndDate(monthTime.getTime());


        boolean res = save(investOrder);
        if (!res) throw new ErrorRequestException("订单生成失败");


        //保存购物车商品信息
        yxInvestOrderCartInfoService.saveCartInfo(investOrder.getId(), cartInfo);
        YxInvestCustomer customer = yxInvestCustomerService.getByPhone(param.getInvestPhone());
        if(customer == null && !param.getInvestPhone().equals("")) {
            //保存客户信息
            YxInvestCustomer yxInvestCustomer = new YxInvestCustomer();
            yxInvestCustomer.setSalesManName(userInfo.getRealName());
            yxInvestCustomer.setSalesManId(userInfo.getUid());
            yxInvestCustomer.setAddTime(new Date());
            yxInvestCustomer.setInvestIdcard(param.getInvestIdcard());
            yxInvestCustomer.setInvestName(param.getInvestName());
            yxInvestCustomer.setInvestPhone(param.getInvestPhone());
            yxInvestCustomerService.save(yxInvestCustomer);
        }




        //购物车状态修改
        QueryWrapper<YxInvestCart> wrapper = new QueryWrapper<>();
        wrapper.in("id", cartIds);
        YxInvestCart cartObj = new YxInvestCart();
        cartObj.setIsPay(1);
        yxInvestCartMapper.update(cartObj, wrapper);

        //删除缓存
        delCacheOrderInfo(uid, key);



        //加入redis，30分钟自动取消
//        String redisKey = String.valueOf(StrUtil.format("{}{}",
//                ShopConstants.REDIS_ORDER_OUTTIME_UNPAY, storeOrder.getId()));
//        redisTemplate.opsForValue().set(redisKey, storeOrder.getOrderId(),
//                ShopConstants.ORDER_OUTTIME_UNPAY, TimeUnit.MINUTES);

        return investOrder;
    }

    @Override
    public InvestCacheDTO getCacheOrderInfo(int uid, String key) {

        return (InvestCacheDTO) redisService.getObj("invest_order_" + uid + key);
    }

    @Override
    public void delCacheOrderInfo(int uid, String key) {
        redisService.delete("invest_order_"+uid+key);
    }

    @Override
    public InvestOrderCountDTO orderData(int uid){

        InvestOrderCountDTO countDTO = new InvestOrderCountDTO();
        //获取当前月1号
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.getTime();
        //本月成交单
        QueryWrapper<YxInvestOrder> wrapperOne = new QueryWrapper<>();
        if(uid > 0 ) wrapperOne.eq("sales_man_id",uid);
        wrapperOne.eq("invest_status",3);
        countDTO.setOrderCountMonth(yxInvestOrderMapper.selectCount(wrapperOne));
        //本月成交金额
        countDTO.setSumPriceMonth(yxInvestOrderMapper.sumPrice(uid));

        //订单未支付数量
        QueryWrapper<YxInvestOrder> wrapperTwo = new QueryWrapper<>();
        if(uid > 0 )wrapperTwo.eq("sales_man_id",uid);
        wrapperTwo.eq("paid",0).eq("refund_status",0).ne("invest_status",5);
        countDTO.setUnpaidCount(yxInvestOrderMapper.selectCount(wrapperTwo));

        //待审核数量 数量
        QueryWrapper<YxInvestOrder> wrapperThree = new QueryWrapper<>();
        if(uid > 0 ) wrapperThree.eq("sales_man_id",uid);
        wrapperThree.eq("paid",1).eq("audit_status",0).eq("refund_status",0);
        countDTO.setUnAuditCount(yxInvestOrderMapper.selectCount(wrapperThree));

        //已审核数量 数量
        QueryWrapper<YxInvestOrder> wrapperFourth = new QueryWrapper<>();
        if(uid > 0 ) wrapperFourth.eq("sales_man_id",uid);
        wrapperFourth.eq("paid",1).eq("audit_status",1).eq("refund_status",0).ne("invest_status",3).ne("invest_status",9);
        countDTO.setAuditedCount(yxInvestOrderMapper.selectCount(wrapperFourth));
        //已到期数量 数量/
        QueryWrapper<YxInvestOrder> wrapperFive = new QueryWrapper<>();
        if(uid > 0 ) wrapperFive.eq("sales_man_id",uid);
        wrapperFive.eq("paid",1).eq("audit_status",1).eq("refund_status",0).eq("invest_status",4);
        countDTO.setEndCount(yxInvestOrderMapper.selectCount(wrapperFive));

        //超时未支付数量
        QueryWrapper<YxInvestOrder> wrapperSix = new QueryWrapper<>();
        if(uid > 0 ) wrapperSix.eq("sales_man_id",uid);
        wrapperSix.eq("paid",0).eq("invest_status",5);
        countDTO.setUnpaidEndCount(yxInvestOrderMapper.selectCount(wrapperSix));

        //取消数量
        QueryWrapper<YxInvestOrder> wrapperSeven = new QueryWrapper<>();
        if(uid > 0 ) wrapperSeven.eq("sales_man_id",uid);
        wrapperSeven.eq("invest_status",9);
        countDTO.setCancelCount(yxInvestOrderMapper.selectCount(wrapperSeven));

        //客户数量
        QueryWrapper<YxInvestCustomer> customerWrapper = new QueryWrapper<>();
        if(uid > 0 ) customerWrapper.eq("sales_man_id",uid);
        countDTO.setCustomerCount(yxInvestCustomerMapper.selectCount(customerWrapper));

        //商品保留数据
        YxStoreProductQueryParam productQueryParam = new YxStoreProductQueryParam();
        countDTO.setProductLockCount(yxStoreProductService.getLockProductCount(productQueryParam, uid));




        //浏览数量
        QueryWrapper<YxStoreProductRelation> productBrowseWrapper = new QueryWrapper<>();
        productBrowseWrapper.eq("uid",uid);
        productBrowseWrapper.eq("type","browse");
        countDTO.setBrowseCount(  yxStoreProductRelationMapper.selectCount(productBrowseWrapper));
        //收藏数量
        QueryWrapper<YxStoreProductRelation> productCollectWrapper = new QueryWrapper<>();
        productCollectWrapper.eq("uid",uid);
        productCollectWrapper.eq("type","Collect");
        countDTO.setCollectCount(   yxStoreProductRelationMapper.selectCount(productCollectWrapper) );

        YxSalesCommissionDetailQueryParam yxSalesCommissionDetailQueryParam = new YxSalesCommissionDetailQueryParam();
        yxSalesCommissionDetailQueryParam.setSalesId(uid);
        yxSalesCommissionDetailQueryParam.setBeginDate("20200101");
        yxSalesCommissionDetailQueryParam.setEndDate("20230101");
//        List<YxSalesCommissionDetailQueryVo> commissionList= yxSalesCommissionDetailService.userCommission(yxSalesCommissionDetailQueryParam,);
//        countDTO.setCommissionCount(commissionList.size());

        return countDTO;
    }

    @Override
    public InvestOrderCountDTO orderDataTeam(int uid,  List<Integer> saleId){

        InvestOrderCountDTO countDTO = new InvestOrderCountDTO();
        //获取当前月1号
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date date= calendar.getTime();
        //本月成交单
        QueryWrapper<YxInvestOrder> wrapperOne = new QueryWrapper<>();
        if( saleId == null) wrapperOne.ne("sales_man_id",uid);
        else if(saleId != null){
            wrapperOne.in("sales_man_id",saleId);
        }
        wrapperOne.eq("invest_status",3).ge("add_time",new SimpleDateFormat("yyyyMMdd").format(date));

        countDTO.setOrderCountMonth(yxInvestOrderMapper.selectCount(wrapperOne));
        //本月成交金额
        countDTO.setSumPriceMonth(yxInvestOrderMapper.sumPrice(uid));

        //订单未支付数量
        QueryWrapper<YxInvestOrder> wrapperTwo = new QueryWrapper<>();
        if( saleId == null) wrapperTwo.ne("sales_man_id",uid);
        else if(saleId != null){
            wrapperTwo.in("sales_man_id",saleId);
        }
        wrapperTwo.eq("paid",0).eq("refund_status",0).ne("invest_status",5);
        countDTO.setUnpaidCount(yxInvestOrderMapper.selectCount(wrapperTwo));

        //待审核数量 数量
        QueryWrapper<YxInvestOrder> wrapperThree = new QueryWrapper<>();
        if( saleId == null) wrapperThree.ne("sales_man_id",uid);
        else if(saleId != null){
            wrapperThree.in("sales_man_id",saleId);
        }
        wrapperThree.eq("paid",1).eq("audit_status",0).eq("refund_status",0);
        countDTO.setUnAuditCount(yxInvestOrderMapper.selectCount(wrapperThree));

        //已审核数量 数量
        QueryWrapper<YxInvestOrder> wrapperFourth = new QueryWrapper<>();
         if( saleId == null) wrapperFourth.ne("sales_man_id",uid);
        else if(saleId != null){
             wrapperFourth.in("sales_man_id",saleId);
        }
        List<String> auditStatusList = new ArrayList<String>();
        auditStatusList.add("1");
        auditStatusList.add("2");
        wrapperFourth.eq("paid",1).in("audit_status",auditStatusList).eq("refund_status",0).ne("invest_status",9);
        countDTO.setAuditedCount(yxInvestOrderMapper.selectCount(wrapperFourth));
        //已到期数量 数量/
        QueryWrapper<YxInvestOrder> wrapperFive = new QueryWrapper<>();
        if( saleId == null) wrapperFive.ne("sales_man_id",uid);
        else if(saleId != null){
            wrapperFive.in("sales_man_id",saleId);
        }
        wrapperFive.eq("paid",1).eq("audit_status",1).eq("refund_status",0).eq("invest_status",4);
        countDTO.setEndCount(yxInvestOrderMapper.selectCount(wrapperFive));

        //超时未支付数量
        QueryWrapper<YxInvestOrder> wrapperSix = new QueryWrapper<>();
        if( saleId == null) wrapperSix.ne("sales_man_id",uid);
        else if(saleId != null){
            wrapperSix.in("sales_man_id",saleId);
        }
        wrapperSix.eq("paid",0).eq("invest_status",5);
        countDTO.setUnpaidEndCount(yxInvestOrderMapper.selectCount(wrapperSix));

        //取消数量
        QueryWrapper<YxInvestOrder> wrapperSeven = new QueryWrapper<>();
        if( saleId == null) wrapperSeven.ne("sales_man_id",uid);
        else if(saleId != null){
            wrapperSeven.in("sales_man_id",saleId);
        }
        wrapperSeven.eq("invest_status",9);
        countDTO.setCancelCount(yxInvestOrderMapper.selectCount(wrapperSeven));

        //客户数量
        QueryWrapper<YxInvestCustomer> customerWrapper = new QueryWrapper<>();
        if( saleId == null) customerWrapper.ne("sales_man_id",uid);
        else if(saleId != null){
            customerWrapper.in("sales_man_id",saleId);
        }
        countDTO.setCustomerCount(yxInvestCustomerMapper.selectCount(customerWrapper));

        //商品保留数据
        YxStoreProductQueryParam productQueryParam = new YxStoreProductQueryParam();
        countDTO.setProductLockCount(yxStoreProductService.getLockProductCount(productQueryParam, uid));

        //浏览数量
        QueryWrapper<YxStoreProductRelation> productBrowseWrapper = new QueryWrapper<>();
        productBrowseWrapper.eq("uid",uid);
        productBrowseWrapper.eq("type","browse");
        countDTO.setBrowseCount(  yxStoreProductRelationMapper.selectCount(productBrowseWrapper));
        //收藏数量
        QueryWrapper<YxStoreProductRelation> productCollectWrapper = new QueryWrapper<>();
        productCollectWrapper.eq("uid",uid);
        productCollectWrapper.eq("type","Collect");
        countDTO.setCollectCount(   yxStoreProductRelationMapper.selectCount(productCollectWrapper) );

        YxSalesCommissionDetailQueryParam yxSalesCommissionDetailQueryParam = new YxSalesCommissionDetailQueryParam();
        yxSalesCommissionDetailQueryParam.setSalesId(uid);
        yxSalesCommissionDetailQueryParam.setBeginDate("20200101");
        yxSalesCommissionDetailQueryParam.setEndDate("20230101");
        List<YxSalesCommissionDetailQueryVo> commissionList= yxSalesCommissionDetailService.userCommission(uid,yxSalesCommissionDetailQueryParam,saleId);
        countDTO.setCommissionCount(commissionList.size());






        return countDTO;
    }


    /**
     * 订单列表
     * @param uid
     * @param type
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<YxInvestOrderQueryVo> orderList(int uid, String investPhone,int type, int page, int limit) {
        QueryWrapper<YxInvestOrder> wrapper= new QueryWrapper<>();
        if(uid > 0  ) wrapper.eq("sales_man_id",uid);
        if(investPhone != null&&investPhone.length() > 0) {
             wrapper.like("invest_phone",investPhone);
        }
        wrapper.orderByDesc("add_time");

        switch (type ){
            case 0://锁定
                wrapper.eq("paid",0).eq("refund_status",0);
                break;
            case 1://未支付
                wrapper.eq("paid",0).eq("refund_status",0).ne("invest_status",5);
                break;
            case 2://待审核
            wrapper.eq("paid",1).eq("refund_status",0).eq("audit_status",0);
            break;
            case 3://投资中
                List<String> auditStatusList = new ArrayList<String>();
                auditStatusList.add("1");
                auditStatusList.add("2");
                wrapper.eq("paid",1).eq("refund_status",0).in("audit_status",auditStatusList).eq("invest_status",3);
                break;
            case 4://已到期
                wrapper.eq("paid",1).eq("refund_status",0).eq("invest_status",4);
                break;
            case 5://超时未支付
                wrapper.eq("paid",0).eq("refund_status",0).eq("invest_status",5);
                break;
            case 9://取消
                wrapper.eq("invest_status",9);
                break;

        }

        Page<YxInvestOrder> pageModel = new Page<>(page, limit);

        IPage<YxInvestOrder> pageList = yxInvestOrderMapper.selectPage(pageModel,wrapper);
        List<YxInvestOrderQueryVo> list = investOrderMap.toDto(pageList.getRecords());
         List<YxInvestOrderQueryVo> newList = new ArrayList<>();
        for (YxInvestOrderQueryVo order : list) {
            YxInvestOrderQueryVo orderQueryVo = handleOrder(order);
            newList.add(orderQueryVo);
        }

        return list;
    }

    /**
     * 订单列表(团队)
     * @param uid
     * @param type
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<YxInvestOrderQueryVo> orderListTeam(int uid, String investPhone,int type, int page, int limit, List<Integer> saleId) {
        QueryWrapper<YxInvestOrder> wrapper= new QueryWrapper<>();
        if(saleId != null && uid >0){
            wrapper.in("sales_man_id",saleId);
        }
        else if(saleId == null) wrapper.ne("sales_man_id",uid);
        if(investPhone != null&&investPhone.length() > 0) {
            wrapper.like("invest_phone",investPhone);
        }

        wrapper.orderByDesc("add_time");

        switch (type ){
            case 0://锁定
                wrapper.eq("paid",0).eq("refund_status",0);
                break;
            case 1://未支付
                wrapper.eq("paid",0).eq("refund_status",0).ne("invest_status",5);
                break;
            case 2://待审核
                wrapper.eq("paid",1).eq("refund_status",0).eq("audit_status",0);
                break;
            case 3://投资中
                List<String> auditStatusList = new ArrayList<String>();
                auditStatusList.add("1");
                auditStatusList.add("2");
                wrapper.eq("paid",1).eq("refund_status",0).in("audit_status",auditStatusList).eq("invest_status",3);
                break;
            case 4://已到期
                wrapper.eq("paid",1).eq("refund_status",0).eq("invest_status",4);
                break;
            case 5://超时未支付
                wrapper.eq("paid",0).eq("refund_status",0).eq("invest_status",5);
                break;
            case 9://取消
                wrapper.eq("invest_status",9);
                break;

        }

        Page<YxInvestOrder> pageModel = new Page<>(page, limit);

        IPage<YxInvestOrder> pageList = yxInvestOrderMapper.selectPage(pageModel,wrapper);
        List<YxInvestOrderQueryVo> list = investOrderMap.toDto(pageList.getRecords());
        List<YxInvestOrderQueryVo> newList = new ArrayList<>();
        for (YxInvestOrderQueryVo order : list) {
            YxInvestOrderQueryVo orderQueryVo = handleOrder(order);
            newList.add(orderQueryVo);
        }

        return list;
    }

    /**
     * 处理订单返回的状态
     * @param order order
     * @return
     */
    @Override
    public YxInvestOrderQueryVo handleOrder(YxInvestOrderQueryVo order) {
        QueryWrapper<YxInvestOrderCartInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",order.getId());
        List<YxInvestOrderCartInfo> cartInfos = yxInvestOrderCartInfoService.list(wrapper);

        List<YxStoreCartQueryVo> cartInfo = new ArrayList<>();
        for (YxInvestOrderCartInfo info : cartInfos) {
            YxStoreCartQueryVo cartQueryVo = JSON.parseObject(info.getCartInfo(),YxStoreCartQueryVo.class);
            cartQueryVo.setUnique(info.getUnique());
            cartInfo.add(cartQueryVo);
        }
        order.setCartInfo(cartInfo);
        return order;
    }



}
